package dk.hojlind.hackhunt.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import dk.hojlind.hackhunt.control.RegistryHandler;

public class RegistryDialog extends JDialog{
	
	//Public variables
	
	//Private variables
	RegistryHandler handler;
	String[] hat = { "WhiteHat", "Blackhat"};
	String result;

	public RegistryDialog(RegistryHandler handler)
	{
		this.handler = handler;
		initUI();
	}

	private void initUI()
	{
		final JFrame frame = new JFrame();
		//Called to position components absolutely
		setLayout(null);
		
		//Set size and behavior of dialog
		setTitle("Register for Hack Hunt");
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setModalityType(ModalityType.APPLICATION_MODAL);
		setLocationRelativeTo(null);
		setSize(300, 550);

		//Create labels and textfields
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(20, 20, 240, 30);
		
		final JTextField txtName = new JTextField();
		txtName.setBounds(20, 50, 240, 30);
		
		JLabel lblUser = new JLabel("Username");
		lblUser.setBounds(20, 100, 240, 30);
		
		final JTextField txtUser = new JTextField();
		txtUser.setBounds(20, 130, 240, 30);
		
		JLabel lblPass = new JLabel("Password");
		lblPass.setBounds(20, 180, 240, 30);
		
		final JPasswordField txtPass = new JPasswordField();
		txtPass.setBounds(20, 210, 240, 30);
		
		JLabel lblTeam = new JLabel("Team Name");
		lblTeam.setBounds(20, 260, 240, 30);
		
		final JTextField txtTeam = new JTextField();
		txtTeam.setBounds(20, 290, 240, 30);
		
		JLabel lblEvil = new JLabel("Are you white- or blackhat?");
		lblEvil.setBounds(20, 340, 240, 30);
		
		final JComboBox isEvil = new JComboBox(hat);
		isEvil.setBounds(20, 370, 240, 30);
		
		JButton accept = new JButton("Accept");
		accept.setBounds(160, 470, 100, 30);
		
		accept.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				
				//MUST CHANGE THE WAY THE PASSWORD IS HANDLED!!!
				result = handler.addUser(txtName.getText(), txtUser.getText(), txtPass.getText(), txtTeam.getText(), String.valueOf(isEvil.getSelectedItem()));
				
				if(!result.contains("error"))
				{
					dispose();
				}
				else
				{
					JOptionPane.showMessageDialog(frame, result);
				}
			}
		});

		JButton cancel = new JButton("Cancel");
		cancel.setBounds(20, 470, 100, 30);
		
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				dispose();
			}
		});

		
		//Add everything
		add(lblName);
		add(txtName);
		add(lblUser);
		add(txtUser);
		add(lblPass);
		add(txtPass);
		add(lblTeam);
		add(txtTeam);
		add(lblEvil);
		add(isEvil);
		add(accept);
		add(cancel);
		
	}

}
